#include "bsp_temp_humi_handler.h"
#include "bsp_aht21_driver.h"

#define OS_QUEUE_CREATE handler_instance->os_interface->pfqueue_create
#define MAX_DELAY       100//0xFFFFFFFFUL
//实例化handler对象为全局变量
static bsp_temp_humi_handle_t bsp_temp_humi_handle_instance = 
{
    .inited = false
};

/*初始化handler*/
static int8_t temp_humi_handler_init(bsp_temp_humi_handle_t *handler_instance)
{   
    int8_t ret = 0;
    //1. 创建队列,句柄赋值给handler
    ret = OS_QUEUE_CREATE(10, sizeof(temp_humi_handler_event_t), &(handler_instance->event_queue_handler));
    printf("sizeof(temp_humi_handler_event_t):%d\r\n",sizeof(temp_humi_handler_event_t));
    if(ret) return -1;

    //2. 初始化实例aht21或者DHT11(读取id是否成功),赋值给handler
    static bsp_aht21_driver_t aht21_instance;
    ret = inst_driver(&aht21_instance, 
                       handler_instance->iic_interface, 
                       handler_instance->yield_interface, 
                       handler_instance->irq_interface);
    handler_instance->aht21_instance = &aht21_instance;
    if(ret) return -1;
    return 0;
}

/*构造函数*/
int8_t inst_handler(bsp_temp_humi_handle_t *temp_humi_handle, temp_humi_handler_all_input_arg_t *all_args)
{   
    //1. 参数判空
    //2. 填充结构体
    temp_humi_handle->iic_interface   = all_args->iic_driver_interface;
    temp_humi_handle->irq_interface   = all_args->irq_interface;
    temp_humi_handle->yield_interface = all_args->yield_interface;

    temp_humi_handle->os_interface    = all_args->temp_humi_handler_os_interface;
    //3. 赋值本层接口
    //4. 调用temp_humi_handler_init
    temp_humi_handler_init(temp_humi_handle);

    temp_humi_handle->inited = true;
    return 0;
}   
/*对app层提供的函数 */
int8_t bsp_temp_humi_read(temp_humi_handler_event_t *event)
{   
    printf("发送消息内容,新鲜度:%d\r\n",event->lifetime);
    printf("发送消息内容,当前时间%d\r\n",event->timestamp);
    int8_t ret =0;
    //先判断handler是否初始化,有可能app线程启动了handler线程还没启动
    printf("是否初始化:%d\r\n",bsp_temp_humi_handle_instance.inited);
    if(false == bsp_temp_humi_handle_instance.inited)
        return -1;
    
    //写事件消息到队列
	printf("写队列之前 \r\n");
    ret = bsp_temp_humi_handle_instance.os_interface->pfqueue_put(
        bsp_temp_humi_handle_instance.event_queue_handler, event, MAX_DELAY);
    printf("写队列的返回值:%d\r\n",ret);
    if(ret) return -1;
    
    return 0;
}

//线程任务函数
void StartTempHumiTask(void *argument)
{
  /*
  printf("-sss------");
  float humi, temp;
  bsp_aht21_driver_t aht21_driver;
  all_argus_t* all_arg = (all_argus_t*)argument;
  inst_handler(&aht21_driver, all_arg->iic_driver_interface, all_arg->yield_interface, all_arg->irq_interface);
  aht21_driver.pfaht_read(&aht21_driver, &humi, &temp);
  printf("humi:%f,temp:%f",humi,temp);
  */
 int8_t ret = 0;
 temp_humi_handler_event_t event;
 uint32_t time = 0;
 float temp = 0;
 float humi = 0;

 //传递完整参数给handler
 temp_humi_handler_all_input_arg_t * all_args = (temp_humi_handler_all_input_arg_t *)argument;
 inst_handler(&bsp_temp_humi_handle_instance, all_args);

 //初始值 时间、温度、湿度
 bsp_temp_humi_handle_instance.aht21_instance->pfaht_read(bsp_temp_humi_handle_instance.aht21_instance,&humi, &temp);
 time = bsp_temp_humi_handle_instance.os_interface->pfget_tick();
 printf("初始化的温度:%f,湿度:%f,时间:%d\r\n",temp,humi,time);																														
 
  //死循环读取队列消息
  for(;;)
  {
    ret = bsp_temp_humi_handle_instance.os_interface->pfqueue_get(bsp_temp_humi_handle_instance.event_queue_handler, 
                                                                    &event, MAX_DELAY);

    if(ret)continue;  //防止出现读到脏数据
    printf("读队列的返回值:%d\r\n",ret);
    printf("收到的消息新鲜度:%d\r\n",event.lifetime);
    printf("收到的消息当前时间:%d\r\n",event.timestamp);
    if(event.timestamp - time > event.lifetime)
    {
        bsp_temp_humi_handle_instance.aht21_instance->pfaht_read(bsp_temp_humi_handle_instance.aht21_instance,
                                                                   &humi, &temp);
        time = bsp_temp_humi_handle_instance.os_interface->pfget_tick();   
    }

    // //调用回调函数,回传值给app线程
    event.pfcallback(&temp, &humi);
  }
}
